// 设置菜单
if (window.ClientBridge && ClientBridge.setMenus) {
  ClientBridge.setMenus([{
    name: '积分商城',
    content: '/integral'
  }, {
    name: '兑换记录',
    content: '/integral/orders'
  }, {
    name: '兑换说明',
    content: '/integral/instruction'
  }]);
}

// 商品列表
(function () {
  if (location.pathname !== '/integral/goods') {
    return false;
  }

  // 加载更多
  loadMoreFactory({
    url: '/integral/ajax/goods',
    panel: '#goods',
    template: '#goodsTempl'
  });
})();

// 订单列表
(function () {
  if (location.pathname !== '/integral/orders') {
    return false;
  }

  // 加载更多
  loadMoreFactory({
    size: 20,
    url: '/integral/ajax/orders',
    panel: '#orders',
    template: '#ordersTempl'
  });
})();

// 流量包列表
(function () {
  if (location.pathname !== '/integral/packages') {
    return false;
  }

  // 更改手机
  var input = $('input[name="mobile"]');
  var origin = input.val();
  var panel = $('#packages');
  input.on('input', function () {
    var mobile = $(this).val();
    if (/^\d{11}$/.test(mobile)) {
      if (origin === mobile) {
        $('#packages > .item > button').removeClass('disabled');
      }
      else {
        panel.html('<div class="vspace text-sm text-center text-gray" ui-mode="50px">请稍后，正在加载运营商套餐..</div>');
        $.get('/integral/ajax/packages', {
          mobile: mobile
        }).then(function (res) {
          if (res.code === 200) {
            origin = mobile;
            panel.html(Template.compile('#packagesTempl', res.data));
          }
          else {
            panel.html('<div class="vspace text-sm text-center text-gray" ui-mode="50px">加载失败，请重新尝试</div>');
          }
        });
      }
    }
    else {
      $('#packages button').addClass('disabled');
    }
  });

  // 触发兑换
  $(document).on('click', 'button.exchange', function () {
    var data = $(this).data();
    Modal.confirm({
      message: '确认花费<i class="text-orange">' + data.integral + '积分</i>兑换<i class="text-orange">' + data.name + '</i>吗？',
      callback: function () {
        $.post('/integral/ajax/placeorder', {
          goodsId: data.id,
          goodsType: data.type,
          consigneeMobile: $('input[name="mobile"]').val(),
        }).then(function (res) {
          if (res.code === 200) {
            location.href = '/integral/success/' + res.data.entity.orderId;
          }
          else {
            Modal.alert(res.data.message);
          }
        });
      }
    });
  });
})();

// 积分明细
(function () {
  if (location.pathname.indexOf('/integral/records') === -1) {
    return false;
  }

  // 更多明细
  loadMoreFactory({
    size: 20,
    url: '/integral/ajax/details',
    panel: '#details',
    template: '#records',
    before: function () {
      return $('#details').is('.active');
    }
  });

  // 更多收入
  loadMoreFactory({
    size: 20,
    url: '/integral/ajax/records',
    panel: '#inputs',
    template: '#records',
    before: function () {
      return $('#inputs').is('.active');
    }
  });

  // 更多支出
  loadMoreFactory({
    size: 20,
    url: '/integral/ajax/deposit',
    panel: '#outputs',
    template: '#records',
    before: function () {
      return $('#outputs').is('.active');
    }
  });

  // 悬挂选项卡
  var tabs = $('.tabs');
  var offset = tabs.offset();
  if ($('.bar.bar-header').length === 0) {
    tabs.addClass('suspend');
  }
  else {
    // 更改偏移值
    $(window).on('resize', function () {
      offset = tabs.offset();
    });

    $(window).on('scroll', function () {
      tabs.toggleClass('suspend', window.scrollY > offset.top);
    }).triggerHandler('scroll');
  }
})();

// 积分商品
(function () {
  if (/^\/integral\/details\/\w+/.test(location.pathname) === false) {
    return false;
  }

  $(document).on('click', '.expand', function () {
    $('.expand').toggle();
    $('.describe').html($('.describe .lazyhtml').val());
    var s = document.body.scrollTop;
    var e = $('.describe').offset().top - 20;
    var move = function (i) {
      document.body.scrollTop = s + (e - s) / 20 * i;
    };
    for (var i = 1; i <= 20; i++) {
      setTimeout(move, i * 15, i);
    }
  });
})();

// 新增地址
(function () {
  if (/^\/integral\/address/.test(location.pathname) === false) {
    return false;
  }

  $(document).on('submit', 'form[name="address"]', function () {
    var form = this;
    if (form.name.value.trim() === '') {
      notice('请输入收货人姓名');
      return false;
    }
    if (!/\d{11}/.test(form.mobile.value)) {
      notice('请输入正确的手机号码');
      return false;
    }
    if (form.address.value.trim() === '') {
      notice('请输入收货人详细地址');
      return false;
    }

    $('footer button').prop('disabled', true).html('正在保存...');
  });
})();

// 确认兑换(实物)
(function () {
  if (/^\/integral\/confirm\/\w+/.test(location.pathname) === false) {
    return false;
  }

  $(document).on('click', '#submit', function () {
    var button = $(this);
    var goodsId = button.data('goodsId');
    var goodsType = button.data('goodsType');

    var address = $('#address').data();
    if (goodsType === 'ENTITY' && !address) {
      return notice('请选择收货地址');
    }

    button.html('正在提交...').prop('disabled', true);

    // 下单
    $.post('/integral/ajax/placeorder', $.extend(address || {}, {
      goodsId: goodsId,
      goodsType: goodsType,
    })).then(function (res) {
      if (res.code === 200) {
        location.href = '/integral/success/' + res.data.entity.orderId;
      }
      else {
        Modal.alert(res.data.message);
      }
      button.html('立即兑换').prop('disabled', false);
    });
  });
})();
